
package com.njkwt.business.system.service;

import com.njkwt.business.system.domain.UploadFile;
import com.njkwt.business.system.domain.UploadFileExample;
import com.njkwt.business.system.dto.UploadFileDto;
import com.njkwt.business.system.dto.PageDto;
import com.njkwt.business.system.mapper.UploadFileMapper;
import com.njkwt.util.CopyUtil;
import com.njkwt.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Date;

/**
* @ClassName  UploadFileService
* @Description
* @Author ydj (171799383@qq.com)
* @Date 2020-8-18 14:45:23
* @Version V1.0
**/

@Service
public class UploadFileService {

    @Resource
    private UploadFileMapper uploadFileMapper;

    /**
     * 列表查询
     */
    public void list(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        UploadFileExample uploadFileExample = new UploadFileExample();
        List<UploadFile> uploadFileList = uploadFileMapper.selectByExample(uploadFileExample);
        PageInfo<UploadFile> pageInfo = new PageInfo<>(uploadFileList);
        pageDto.setTotal(pageInfo.getTotal());
        List<UploadFileDto> uploadFileDtoList = CopyUtil.copyList(uploadFileList, UploadFileDto.class);
        pageDto.setList(uploadFileDtoList);
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    public void save(UploadFileDto uploadFileDto) {
        UploadFile uploadFile = CopyUtil.copy(uploadFileDto, UploadFile.class);
        if (StringUtils.isEmpty(uploadFileDto.getId())) {
            this.insert(uploadFile);
        } else {
            this.update(uploadFile);
        }
    }

    /**
     * 新增
     */
    private void insert(UploadFile uploadFile) {
        Date now = new Date();
        uploadFile.setId(UuidUtil.getShortUuid());
        uploadFileMapper.insert(uploadFile);
    }

    /**
     * 更新
     */
    private void update(UploadFile uploadFile) {
        uploadFileMapper.updateByPrimaryKey(uploadFile);
    }

    /**
     * 删除
     */
    public void delete(String id) {
        uploadFileMapper.deleteByPrimaryKey(id);
    }
}
